<?php
class cmsdream_cache {
	public $Data = array();
	
	public function get($name, $is_permanence = false) {
		if(isset($this->Data[$name])) {
			return $this->Data[$name];
		}
		//$script = md5($name);
		$script = $name;
		@include($GLOBALS['C']->CACHEDIR.'/'.$script.'.php');
		$expiration = isset($expiration) ? intval($expiration) : 0;
		if($is_permanence) {
			$this->Data[$name] = isset($data) ? $data : null;
		} else {
			$this->Data[$name] = (time() > intval($expiration)) ? null : $data;
		}
		return $this->Data[$name];
	}
	
	public function write($script, $data, $expiration = 0) {
		//$script = md5($script);
		$script = $script;
		if(!$this->mkdir($GLOBALS['C']->CACHEDIR)) return;
		if(@$fp = fopen($GLOBALS['C']->CACHEDIR.'/'.$script.".php", 'w')) {
			$str  = "<?php\n";
			$str .= "//Created at ".date("Y-m-d H:i:s")."\n";
			$str .= "//2008-".date("Y")." (C) cmsream.com\n";
			$str .= "\$expiration = ".$expiration.";\n";
			$str .= "\$data = unserialize('".$data."');\n";
			$str .= "?>";
			fwrite($fp, $str);
			fclose($fp);
		}
	}
	
	private function mkdir($dir) {
		$dir = preg_replace("/[\*\|\"\?\>\<\/]+/i", "/", $dir);
		if(!is_dir($dir)){
			if(!$this->mkdir(dirname($dir))){
				return false;
			}
			if(!mkdir($dir,0777)){
				return false;
			}
		}
		return true;
	}

	public function get_post_uri($id) {
		$db = $GLOBALS['DB'];
		$tb = $GLOBALS['C']->TABLE_PREFIX;
		$row = $db->fetch("SELECT ID,post_name,post_parent FROM {$tb}posts WHERE ID = $id AND post_type IN ('post','attachment')");
		if($row) {
			if(0 == $row->post_parent) {
				return $row->ID;
			} else {
				return $this->get_post_uri($row->post_parent) . "/" . $row->post_name;
			}
		}
	}
	
	public function get_page_uri($id) {
		$db = $GLOBALS['DB'];
		$tb = $GLOBALS['C']->TABLE_PREFIX;
		$uri = '';
		$row = $db->fetch("SELECT ID,post_name,post_parent FROM {$tb}posts WHERE ID = $id AND post_type = 'page'");
		if($row) {
			$uri = $row->post_name;
			if($row->ID == $row->post_parent) {
				return $uri;
			} else {
				$uri = $this->get_page_uri($row->post_parent) . "/" . $uri;
			}
		}
		return $uri;
	}
	
	public function get_tag_uri($id) {
		$db = $GLOBALS['DB'];
		$tb = $GLOBALS['C']->TABLE_PREFIX;
		$uri = '';
		$row = $db->fetch("SELECT `slug` FROM {$tb}terms WHERE term_id = $id");
		if($row) {
			$uri = $row->slug;
		}
		return $uri;
	}

	public function get_category_uri($id) {
		$db = $GLOBALS['DB'];
		$tb = $GLOBALS['C']->TABLE_PREFIX;
		$uri = '';
		$row = $db->fetch("SELECT t.term_id AS ID,t.`slug`,x.`parent` AS P FROM {$tb}terms t 
						LEFT JOIN {$tb}term_taxonomy x ON t.term_id = x.term_id 
						WHERE x.taxonomy = 'category' AND t.term_id = $id");
		if($row) {
			$uri = $row->slug;
			if($row->ID == $row->P) {
				return $uri;
			} else {
				$uri = $this->get_category_uri($row->P) . "/" . $uri;
			}
		}
		return $uri;
	}
	
	public function get_terms($c = 'category', $parent = 0) {
		$db = $GLOBALS['DB'];
		$tb = $GLOBALS['C']->TABLE_PREFIX;
		$arr = array();
		$sql = "SELECT x.term_id AS ID,x.name,t.description,t.count,
				(SELECT COUNT(*) FROM {$tb}term_taxonomy AS p WHERE p.parent = t.term_id) AS `sub`
				FROM {$tb}term_taxonomy AS t 
				LEFT JOIN {$tb}terms AS x ON x.term_id = t.term_id WHERE t.taxonomy = '$c' AND t.parent = $parent ORDER BY x.term_id ASC";
		foreach($db->fetch_all("$sql") as $key => $row) {
			$arr[$key] = new stdClass;
			$arr[$key]->ID = $row->ID;
			$arr[$key]->name = htmlspecialchars($row->name);
			$arr[$key]->description = htmlspecialchars($row->description);
			$arr[$key]->count = $row->count;
			$arr[$key]->url = $this->url($c, $row->ID);
			$arr[$key]->children = ($row->sub > 0) ? $this->get_terms($c, $row->ID) : NULL;
		}
		return $arr;
	}
	
	public function _get_nav_menus_rebuild($arr, $p = 0) {
		$m = array();
		foreach($arr as $key => $row) {
			if($row['parent'] == $p) {
				$m[$key] = new stdClass;
				$m[$key]->key = $row['key'];
				$m[$key]->title = htmlspecialchars($row['title']);
				$m[$key]->url = $row['url'];
				$m[$key]->description = htmlspecialchars($row['description']);
				$m[$key]->target = $row['target'];
				$m[$key]->children = $this->_get_nav_menus_rebuild($arr, $row['key']);
			}
		}
		return !empty($m) ? $m : null;
	}
	
	public function __construct() {
		
	}
	
	public function __destruct(){
	
	}
	
}

